import 'dart:io';

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:extended_plugin/extended_plugin.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  late List<StreamSubscription> subscriptions;

  String str = '----';

  @override
  void initState() {
    super.initState();
    subscriptions = [
      ExtendedPlugin.keyBoardOnChange$.listen((e) {
        print("jieshou daoshuju :$e");
      }),
      ExtendedPlugin.networkStatus$.listen((event) {
        str = event;
        print('event  ${event}');
        setState(() {});
      })
    ];
  }

  @override
  void dispose() {
    super.dispose();
    subscriptions.forEach((s) => s.cancel());
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
          actions: <Widget>[
            InkWell(
              onTap: () {
                FocusScope.of(context).unfocus();
              },
              child: Container(
                height: 44,
                width: 44,
                child: Icon(Icons.cancel),
              ),
            )
          ],
        ),
        body: Center(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              TextField(),
              InkWell(
                onTap: () {
                  print('event  ${str}');
                },
                child: Container(
                  height: 55,
                  width: 120,
                  color: Colors.red,
                  child: Text('${str}'),
                ),
              ),
              TestPage(),
            ],
          ),
        ),
      ),
    );
  }
}

class TestPage extends StatefulWidget {
  @override
  _TestPageState createState() => _TestPageState();
}

class _TestPageState extends State<TestPage> {
  late StreamSubscription subscription;
  @override
  void initState() {
    subscription = ExtendedPlugin.screenShot$.listen((data) {
      showDialog(
        context: context,
        builder: (_) => Center(
          child: Container(
            width: 280,
            child: AspectRatio(
              aspectRatio: 192 / 108,
              child: Image.file(File(data)),
            ),
          ),
        ),
      );
    });
    super.initState();
  }

  @override
  void dispose() {
    subscription.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text("hello"),
    );
  }
}
